define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'purchase/index' + location.search,
                    add_url: 'purchase/add',
                    edit_url: 'purchase/edit',
                    del_url: 'purchase/del',
                    multi_url: 'purchase/multi',
                    import_url: 'purchase/import',
                    table: 'purchase',
                }
            });

            var table = $("#table");
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone,.btn-edit,.btn-one,.btn-add").data("area", ["90%", "90%"]);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true, operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'clothbank_id', title: __('Clothbank_id'), sortable: true, formatter: Table.api.formatter.status, searchList: Config.Clothbank},
                        {field: 'sn', title: __('Sn')},
                        {field: 'total_nums', title: __('Total_nums'), sortable: true},
                        {field: 'total_price', title: __('Total_price'), sortable: true, operate:'BETWEEN'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'remark', title: __('Remark'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'is_settle', title: __('是否结算'), sortable: true, searchList: {"1":__('Yes'),"0":__('No')}, formatter: Table.api.formatter.toggle},
                        {field: 'admin_id', title: __('Admin_id'), sortable: true, formatter: Table.api.formatter.status, searchList: Config.Admins},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'purchase/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'purchase/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'purchase/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
            $(document).on("change keyup", ".nums", function (e) {
            	var total_nums = 0;
            	$(".nums").each(function(i, n){
            		total_nums += Number($("#nums_" + i).val());
                });
            	$("input[name='row[total_nums]']").val(total_nums);
            });
            $(document).on("change keyup", ".price", function (e) {
            	var total_nums = 0;
            	var total_price = 0;
            	$(".price").each(function(i, n){
            		total_price += Number($("#price_" + i).val());
                });
            	$("input[name='row[total_price]']").val(total_price);
            });
        },
        edit: function () {
            Controller.api.bindevent();
            $(document).on("change keyup", ".nums", function (e) {
            	var total_nums = 0;
            	$(".nums").each(function(i, n){
            		total_nums += Number($("#nums_" + i).val());
                });
            	$("input[name='row[total_nums]']").val(total_nums);
            });
            $(document).on("change keyup", ".price", function (e) {
            	var total_nums = 0;
            	var total_price = 0;
            	$(".price").each(function(i, n){
            		total_price += Number($("#price_" + i).val());
                });
            	$("input[name='row[total_price]']").val(total_price);
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
